package com.kehutong.auth.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kehutong.auth.entity.AccountLoginLog;
import com.kehutong.auth.enums.LoginMethod;
import com.kehutong.auth.service.AccountLoginLogService;
import com.kehutong.auth.util.IPSeeker;
import com.kehutong.common.IpUtils;
import com.kehutong.common.enums.UserType;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;

import com.kehutong.auth.service.LoginCustomerService;
import com.kehutong.auth.service.LoginEmployeeService;
import com.kehutong.auth.service.SmsCodeService;
import com.kehutong.common.util.Token;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;

/**
 * 授权登录
 *
 * @author: liukunlong
 * @date: 2021-03-25 19:19
 */
@MvcAction
@ReqMapping("/kht-bin")
public class LoginAction {

    private static Logger logger = LoggerFactory.getLogger(LoginAction.class);

    @Inject
    private LoginEmployeeService loginEmployeeService;
    @Inject
    private LoginCustomerService loginCustomerService;
    @Inject
    private SmsCodeService smsCodeService;

    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private AccountLoginLogService accountLoginLogService;

    @ReqMapping("/isInit")
    public Object isInit(Token token, String corpId, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        return loginEmployeeService.isLogin(token, corpId, req, resp);
    }

    @ReqMapping("/isLogin")
    public Object isLogin(Token token, String corpId, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        return loginEmployeeService.isLogin(token, corpId, req, resp);
    }

    // 员工账号密码登陆
    @ReqMapping("/app/login")
    public Object loginByApp(HttpServletRequest req, String username, String password) throws Exception {
        return loginEmployeeService.logingByPass(req, username, password);
    }

    // 客户账号密码登陆
    @ReqMapping("/client/login")
    public Object loginByClient(String username, String passwd) throws Exception {
        logger.trace("username:{}, passwd:{}", username, passwd);

        return loginCustomerService.loginByMini(null);
    }

    // 企业微信oauthor2.0认证
    @ReqMapping("/tencent/wework/login")
    public Object loginByTencentWework(HttpServletRequest req, String suiteId, String code) throws Exception {
        logger.trace("corpid:{}, code:{}, token:{}", suiteId, code);
        return loginEmployeeService.loginByTencentWework(req, suiteId, code, UserType.webwork);
    }

    // 小程序登陆
    @ReqMapping("/tencent/mn/login")
    public Object loginByMiniProgram(HttpServletRequest req, JSONObject jsonObject) throws Exception {
        logger.trace("loginByMini:{}", jsonObject);

        Object result = loginCustomerService.loginByMini(jsonObject);
        //如果返回的对象是token则插入登录日志
        if (result instanceof Token) {
            Token token = (Token) result;
            String ip=IpUtils.getAddr(req);
            jsonObject.put("ip", ip);
            AccountLoginLog log = Pool.newInstance(AccountLoginLog.class);
            log.setAccountNo(token.getUuid());
            log.setIp(ip);
            IPSeeker ipSeeker = new IPSeeker();
            log.setAddress(ipSeeker.getIPLocation(ip).getCountry());
            log.setLoginMethod(LoginMethod.MINI_PROGRAM);
            log.setRemark("登录成功");
            accountLoginLogService.save(log);
            return token;
        }
        return result;
    }

    // 公众号登陆
    @ReqMapping("/tencent/mp/login")
    public Object loginByMp(JSONObject jsonObject) throws Exception {
        logger.trace("loginByMini:{}", jsonObject);
        return loginCustomerService.loginByMp(jsonObject);
    }

    // 发送短信
    @ReqMapping("/sendsms")
    public Object sendsms(Token token, String mobile) throws Exception {
        return smsCodeService.send(token, mobile);
    }

    // 发送短信
    @ReqMapping("/doTest")
    public Object doTest(Token token) throws Exception {
        logger.trace("{}", token);
        return token;
    }
}
